// index.js
Page({
  data: {
    todoList: [],
    todoFinished: [],
    levels: [
      {name: '紧急的', value: 'urgent', color: 'red'},
      {name: '重要不紧急的', value: 'important', color: 'orange'},
      {name: '不紧急的', value: 'notUrgent', color: '#5872DE', checked: true}
    ],
    addShowlFlag: false,
    addText: '',
    addLevel: 'notUrgent',
    addSubmitDisabled: true,
    inputTips: '内容不允许为空'
  },
  saveData() {
    wx.setStorageSync('storage', {
      todoList: this.data.todoList,
      todoFinished: this.data.todoFinished,
      id_auto_increment: this.data.id_auto_increment > 0 ? this.data.id_auto_increment : 1
    })
  },
  onLoad() {
    var data = wx.getStorageSync('storage')
    this.setData({
      todoList: data.todoList || [],
      todoFinished: data.todoFinished || [],
      id_auto_increment: data.id_auto_increment || 1
    })
  },
  todoListTap(e) {
    const item = this.data.todoList.find(obj => obj.id == e.currentTarget.dataset.id)
    item.checked = true
    const res = this.data.todoList.filter(obj => obj.id != e.currentTarget.dataset.id)
    this.data.todoFinished.push(item)
    this.setData({
      todoList: res,
      todoFinished: this.data.todoFinished
    })
    this.saveData()
  },
  finishedTodoTap(e) {
    const item = this.data.todoFinished.find(obj => obj.id == e.currentTarget.dataset.id)
    item.checked = false
    const res = this.data.todoFinished.filter(obj => obj.id != e.currentTarget.dataset.id)
    this.data.todoList.push(item)
    this.setData({
      todoFinished: res,
      todoList: this.data.todoList
    })
    this.saveData()
  },
  setInput(e) {
    this.setData({addText: e.detail.value})
    if(this.data.addText.trim() != "") {
      this.setData({addSubmitDisabled: false})
    } else {
      this.setData({addSubmitDisabled: true})
    }
  },
  addSubmit() {
    const arr = this.data.todoList
    arr.push({
      id: this.data.id_auto_increment++,
      content: this.data.addText,
      level: this.data.addLevel,
      checked: false
    })
    console.log("add: ", arr)
    this.setData({
      todoList: arr,
      addText: '',
      addLevel: 'notUrgent',
      addShowlFlag: false,
      levels: this.data.levels,
      addSubmitDisabled: true
    })
    wx.showToast({
      title: 'SUCESS',
      icon: 'success',
      duration: 2000
    })
    this.saveData()
  },
  addCancel() {
    this.setData({
      addShowlFlag: false
    })
  },
  addShow() {
    this.setData({
      addShowlFlag: true
    })
  },
  clearAll() {
    this.setData({
      todoFinished: [],
      todoList: []
    })
    this.saveData()
  },
  delTodoItem(e) {
    const res = this.data.todoList.filter(obj => obj.id !== e.currentTarget.dataset.id)
    this.setData({todoList: res})
    this.saveData()
  },
  delFinishedTodoItem(e) {
    const res = this.data.todoFinished.filter(obj => obj.id !== e.currentTarget.dataset.id)
    this.setData({todoFinished: res})
    this.saveData()
  },
  radioChange(e) {
    this.setData({addLevel: e.detail.value})
  }
})
